public class Solution203 {

    public static class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode removeElements(ListNode head, int val) {
        while (head!=null && head.val==val){
            head=head.next;
        }
        if (head==null){
            return null;
        }
        ListNode temp=head;
        while (temp!=null) {
            while (temp.next != null && temp.next.val == val) {
                temp.next = temp.next.next;
            }
            temp=temp.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode=new ListNode(1,new ListNode(2,new ListNode(6,new ListNode(3,new ListNode(4,new ListNode(5,new ListNode(6)))))));
        System.out.println(new Solution203().removeElements(listNode,6).next.next.next.next.val);
    }
}
